/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.subscriptionpackage;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;

/**
 *
 * @author bluejack
 */
public class SubscriptionHandler {
    Hashtable subscriptions = null;
    public SubscriptionHandler() {
        subscriptions = new Hashtable();
    }
    public HashSet getSubscribers(Subscription subscription) {
        return (HashSet) subscriptions.get(subscription);
    }
    
    public Enumeration getSubscriptions() {
        return subscriptions.keys();
    }
    
    public void subscribe(String subscriber, Subscription subscription) {
        HashSet subscribers=getSubscribers(subscription);
        if(subscribers==null) {
            subscribers = new HashSet();//(HashSet) Collections.synchronizedSet(new HashSet());
        }
        subscribers.add(subscriber);
        subscriptions.put(subscription, subscribers);
    }
    
    public void unsubscribe(String subscriber, Subscription subscription) {
        HashSet subscribers=getSubscribers(subscription);
        if(subscribers!=null) {
            subscribers.remove(subscriber);
        }
    }
}
